package example.service;

import example.item.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import example.dao.daoRepository;
import example.exception.adminException;

@Service
public class AService {
    @Autowired
    daoRepository daoRepository;

    //添加代办
    public Item addItem(Item item){
        daoRepository.saveAndFlush(item);
        return daoRepository.findById(item.getId()).get();
    }

    public Item getItem(long id)  throws adminException {
        if(daoRepository.existsById(id)==false){
            throw new adminException("不存在该物品");
        }
        return daoRepository.findById(id).get();
    }



    public void updateItem(long id, Item item)  throws adminException {
        if(daoRepository.existsById(id)==false){
            throw new adminException("不存在该物品");
        }
        daoRepository.saveAndFlush(item);
    }

    public void deleteItem(long id) throws adminException {
        if(daoRepository.existsById(id)==false){
            throw new adminException("不存在该物品");
        }
        daoRepository.deleteById(id);
    }

}
